<script setup lang="ts" name="ApiAccessLogDetail">
import { DICT_TYPE } from '@/enums/dictEnums'
import { formatDate } from '@/utils/dateUtil'
import type { ApiAccessLogType } from '@/api/infra/apiLog/access'
import { setting } from '@/utils/axios/setting'

const { t } = useI18n() // 国际化
const visible = ref(false) // 弹窗的是否展示
const detailData = ref({} as ApiAccessLogType) // 详情数据

/** 打开弹窗 */
const open = async (data: ApiAccessLogType) => {
  visible.value = true
  detailData.value = data
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>

<template>
  <Dialog v-model="visible" :title="t('common', 'detail')" type="scroll" width="800px" height="500px">
    <el-descriptions :column="1" border>
      <el-descriptions-item :label="t('infra.apiLog', 'id')" min-width="120">
        {{ detailData.id }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.apiLog', 'traceId')">
        {{ detailData.traceId }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.apiLog', 'applicationName')">
        {{ detailData.applicationName }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.apiLog', 'userInfo')">
        {{ detailData.userId }}
        <dict-tag :type="DICT_TYPE.USER_TYPE" :value="detailData.userType" />
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.apiLog', 'userIp')">
        {{ detailData.userIp }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.apiLog', 'userAgent')">
        {{ detailData.userAgent }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.apiLog', 'requestInfo')">
        <el-tag>{{ detailData.requestMethod }}</el-tag>
        {{ detailData.requestUrl }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.apiLog', 'requestParams')">
        {{ detailData.requestParams }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.apiLog', 'requestResult')">
        {{ detailData.requestResult }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.apiLog.access', 'beginTime')"> {{ formatDate(detailData.beginTime) }} ~ {{ formatDate(detailData.endTime) }} </el-descriptions-item>
      <el-descriptions-item :label="t('infra.apiLog.access', 'duration')">{{ detailData.duration }} ms</el-descriptions-item>
      <el-descriptions-item :label="t('infra.apiLog.access', 'result')">
        <el-tag v-if="detailData.resultCode === setting.result_success_code" type="success">{{ t('status', 'success') }}</el-tag>
        <template v-else>
          <el-tag type="danger">{{ t('status', 'fail') }}</el-tag>
          | {{ detailData.resultCode }} | {{ detailData.resultMsg }}
        </template>
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.apiLog', 'operateModule')">
        {{ detailData.operateModule }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.apiLog', 'operateType')">
        <dict-tag :type="DICT_TYPE.INFRA_OPERATE_TYPE" :value="detailData.operateType" />
      </el-descriptions-item>
    </el-descriptions>
  </Dialog>
</template>

<style scoped lang="scss"></style>
